package com.bawei.liuzhanlin20210806.view.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bawei.liuzhanlin20210806.R;
import com.bawei.liuzhanlin20210806.model.bean.ShouyeBean;
import com.bawei.liuzhanlin20210806.model.utils.HttpUtils;
import com.bawei.liuzhanlin20210806.view.activity.Main2Activity;
import com.stx.xhb.xbanner.XBanner;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * A simple {@link Fragment} subclass.
 */
public class SyFragment extends Fragment {

    @BindView(R.id.tv_cs)
    TextView tvCs;
    @BindView(R.id.tv_ds)
    TextView tvDs;
    @BindView(R.id.tv_tq)
    TextView tvTq;
    @BindView(R.id.tv_qk)
    TextView tvQk;
    @BindView(R.id.xbann)
    XBanner xbann;
    @BindView(R.id.ll_xl)
    LinearLayout llXl;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_sy, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //展示数据
        HttpUtils.getInstance().getApiService().syservice()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<ShouyeBean>() {
                    @Override
                    public void accept(ShouyeBean shouyeBean) throws Exception {
                        tvCs.setText(shouyeBean.data.result.get(0).city);
                        tvDs.setText(shouyeBean.data.result.get(0).somatosensoryTemperature);
                        tvTq.setText(shouyeBean.data.result.get(0).condition);
                        tvQk.setText(shouyeBean.data.result.get(0).somatosensory + shouyeBean.data.result.get(0).wind2 + "湿度:" + shouyeBean.data.result.get(0).humidity);

                        List<ShouyeBean.DataDTO.ResultDTO.BannerDTO.BannersDTO> banner = shouyeBean.data.result.get(2).banner.banner;
                        xbann.setData(banner, null);
                        xbann.loadImage(new XBanner.XBannerAdapter() {
                            @Override
                            public void loadBanner(XBanner banner, Object model, View view, int position) {
//                                Glide.with(getActivity()).load(banner.get(position).image).into((ImageView) view);
                            }
                        });
                    }
                });
        //点击跳转
        llXl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), Main2Activity.class);
                getActivity().startActivity(intent);
            }
        });
    }
}
